//
//  EditorView.m
//  LabanBrowser
//
//  Created by Xh2 on 3/24/13.
//  Copyright (c) 2013 VNG. All rights reserved.
//

#import "EditorView.h"
#import "StaticVariables.h"
@implementation EditorView{
    UIImageView* backgroundView;
}

double kFadeDuration = 0.3f;
double kShadowAlpha=0.7f;
@synthesize delegate;
@synthesize currentEditingModel;
@synthesize currentEditorType;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    
    }
    return self;
}
-(void)relocalize{
    [self.okBT setTitle:[MyUtilities getLocalizeStringFromTag:@"ActionOK"] forState:UIControlStateNormal];
    [self.cancelBT setTitle:[MyUtilities getLocalizeStringFromTag:@"ActionCancel"] forState:UIControlStateNormal];
    [backgroundView removeFromSuperview];
    [self.contentTitleLB setText:[MyUtilities getLocalizeStringFromTag:@"EditorTitle"]];
    //self.contentTitleLB.shadowColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.7f];
    //self.contentTitleLB.shadowOffset = CGSizeMake(1.0, 2.0);

    
    CGRect backgroundRect=CGRectMake(0, 0, self.contentView.frame.size.width, self.contentView.frame.size.height);
    backgroundView=[[UIImageView alloc]initWithFrame:backgroundRect];
    [backgroundView setImage:[[UIImage imageNamed:@"bg_browser_titlebar_urlbar_nor.png"] stretchableImageWithLeftCapWidth:45 topCapHeight:25]];
    [self.contentView addSubview:backgroundView];
    [self.contentView sendSubviewToBack:backgroundView];
    
    
}

-(void)initialize{
    self.alpha=0;
    self.contentView.alpha=0;
    self.shadowView.alpha=0;
    self.titleTF.delegate=self;
    self.urlTF.delegate=self;
    self.shadowView.backgroundColor=[UIColor blackColor];
    [self relocalize];

    self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    self.shadowView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    self.contentView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;

    //self.frame = CGRectMake(0, 0, mainScreenSize.width, mainScreenSize.height);
    //UIImageView* 
    
}
-(void)autoAdjustToFrame:(CGRect)frame{
    self.frame = CGRectMake(0, 0, frame.size.width, frame.size.height);
    self.shadowView.frame = self.frame;
}
-(void)show{
    self.alpha=1;
    [UIView animateWithDuration:kFadeDuration
                          delay:0
                        options:UIViewAnimationOptionCurveEaseInOut
                     animations:^{
                         self.shadowView.alpha=kShadowAlpha;
                         self.contentView.alpha=1;
                         
    } completion:^(BOOL finished) {
        
    }];
}
-(void)hide{
    [self.titleTF resignFirstResponder];
    [self.urlTF resignFirstResponder];
    
    [UIView animateWithDuration:kFadeDuration
                          delay:0
                        options:UIViewAnimationOptionCurveEaseInOut
                     animations:^{
                         self.alpha=0;
                         self.contentView.alpha=0;
                     } completion:^(BOOL finished) {
                         self.alpha=0;
                     }];
}
-(void)receiveModel:(HistoryModel *)_model WithType:(EditorType)_type{

    currentEditingModel=_model;
    currentEditorType=_type;
    self.titleTF.text=_model.title;
    self.urlTF.text=_model.url;

}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

- (IBAction)okAction:(id)sender {
    

    if(currentEditorType==AddBookmark){
        HistoryModel* currentHistory=(HistoryModel*)currentEditingModel;

        BookmarkModel* insertingBookmark=[[BookmarkModel alloc]initWithTitle:self.titleTF.text AndURL:currentHistory.url];

        [[[StaticVariables shareVariables]bookmarkAccess]insert:insertingBookmark];
        if(delegate && [delegate respondsToSelector:@selector(onEditorAddingBookmarkSuccessfully:)]){
            [delegate onEditorAddingBookmarkSuccessfully:insertingBookmark];
        }
        
        
    }else{
        if(currentEditorType==UpdateBookmark){
            BookmarkModel* edittingBookmark=(BookmarkModel*)currentEditingModel;
            edittingBookmark.title=self.titleTF.text;
            [[[StaticVariables shareVariables]bookmarkAccess]updateObjectAtURL:edittingBookmark.url withNewObject:edittingBookmark];
            if(delegate && [delegate respondsToSelector:@selector(onEditorUpdatingBookmarkSuccessfully:)]){
                [delegate onEditorUpdatingBookmarkSuccessfully:edittingBookmark];
            }
        }else{
            SpeedDialModel* edittingSpeedDial=(SpeedDialModel*)currentEditingModel;
            edittingSpeedDial.title=self.titleTF.text;
            [[[StaticVariables shareVariables]speedDialAccess]updateObjectAtURL:edittingSpeedDial.url withNewObject:edittingSpeedDial];
            if(delegate && [delegate respondsToSelector:@selector(onEditorUpdatingSpeedDialSuccessfully:)]){
                [delegate onEditorUpdatingSpeedDialSuccessfully:edittingSpeedDial];
            }
        }
    }
}
- (IBAction)cancelAction:(id)sender {
    if (delegate && [delegate respondsToSelector:@selector(onEditorCancel)]){
        [delegate onEditorCancel];
    }
}


#pragma mark TextFieldDelegate
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}
-(void)textFieldDidBeginEditing:(UITextField *)textField{
    [textField selectAll:textField];
}

@end
